library(haven)
library(dplyr)
library(MASS)
library(reshape2)
library(brant)
library(psych)
library(ggplot2)
library(waffle)
library(jtools)
library(effects)
library(sandwich)

WV7_Data <- read_dta("WV7_Data.dta")
## WVS Wave 7 HK ##
hk<-filter(WV7_Data, B_COUNTRY==344)

## Define protest, which represents the propensity to protest ##
hk$protest<-NA
hk$protest[which(hk$Q211==3)]<-0
hk$protest[which(hk$Q211==1 | hk$Q211==2)]<-1
hk$protest2<-NA
hk$protest2[which(hk$Q211==3)]<-1
hk$protest2[which(hk$Q211==2)]<-2
hk$protest2[which(hk$Q211==1)]<-3
hk$protest<-as.factor(hk$protest)
hk$protest2<-as.factor(hk$protest2)
table(hk$protest)
table(hk$protest2)

## Define postmat, which represents the postmaterialism index for individual Hongkongers. 0=materialist, 1=mixed values, 2=postmaterialist ##
hk$postmat<-NA
hk$postmat[which((hk$Q154==1 & hk$Q155==3) | (hk$Q154==3 & hk$Q155==1))]<-0
hk$postmat[which((hk$Q154==1 & hk$Q155==2) | (hk$Q154==1 & hk$Q155==4) | (hk$Q154==3 & hk$Q155==2) | (hk$Q154==3 & hk$Q155==4) | (hk$Q154==2 & hk$Q155==1) | (hk$Q154==4 & hk$Q155==1) | (hk$Q154==2 & hk$Q155==3) | (hk$Q154==4 & hk$Q155==3))]<-1
hk$postmat[which((hk$Q154==2 & hk$Q155==4) | (hk$Q154==4 & hk$Q155==2))]<-2
table(hk$postmat)
round(prop.table(table(hk$postmat)),digits = 3)
mean(hk$postmat, na.rm = T)

## Alternative measures for postmaterialism (results not shown)
hk$postmat2<-NA
hk$postmat2[which((hk$Q152==1 & hk$Q153==2) | (hk$Q152==2 & hk$Q153==1))]<-0
hk$postmat2[which((hk$Q152==1 & hk$Q153==3) | (hk$Q152==1 & hk$Q153==4) | (hk$Q152==2 & hk$Q153==3) | (hk$Q152==2 & hk$Q153==4) | (hk$Q152==3 & hk$Q153==1) | (hk$Q152==4 & hk$Q153==1) | (hk$Q152==3 & hk$Q153==2) | (hk$Q152==4 & hk$Q153==2))]<-1
hk$postmat2[which((hk$Q152==3 & hk$Q153==4) | (hk$Q152==4 & hk$Q153==3))]<-2
table(hk$postmat2)
round(prop.table(table(hk$postmat2)),digits = 3)
mean(hk$postmat2, na.rm = T)

hk$postmat3<-NA
hk$postmat3[which((hk$Q156==1 & hk$Q157==4) | (hk$Q156==4 & hk$Q157==1))]<-0
hk$postmat3[which((hk$Q156==1 & hk$Q157==2) | (hk$Q156==1 & hk$Q157==3) | (hk$Q156==4 & hk$Q157==2) | (hk$Q156==4 & hk$Q157==3) | (hk$Q156==2 & hk$Q157==1) | (hk$Q156==3 & hk$Q157==1) | (hk$Q156==2 & hk$Q157==4) | (hk$Q156==3 & hk$Q157==4))]<-1
hk$postmat3[which((hk$Q156==2 & hk$Q157==3) | (hk$Q156==3 & hk$Q157==2))]<-2
table(hk$postmat3)
round(prop.table(table(hk$postmat3)),digits = 3)
mean(hk$postmat3, na.rm = T)

## Define dissat, which represents dissatisfaction with the political system ##
hk$dissat<-NA
hk$Q252<- -1*(hk$Q252-11)
hk$dissat<-hk$Q252

## Define econdissat, which represents dissatisfaction with the finacial situation of his household ##
hk$econdissat<-NA
hk$Q50<- -1*(hk$Q50-11)
hk$econdissat<-hk$Q50

## Define dem, which represents support for democratic system ##
hk$dem<-NA
hk$dem<-hk$Q250
hk$dem<-as.numeric(hk$dem)

## Define polint, which is political interest ##
hk$polint<-NA
hk$Q199<- -1*(hk$Q199-5)
hk$polint<-hk$Q199
## Define poldis, which is political discussion ##
hk$poldis<-NA
hk$Q200<- -1*(hk$Q200-4)
hk$poldis<-hk$Q200

## Define organizational membership ##
hk$organ<-0
hk$organ[which(hk$Q94==1 | hk$Q95==1 | hk$Q96==1 | hk$Q97==1 | hk$Q98==1 | hk$Q99==1 | hk$Q100==1 | hk$Q101==1 | hk$Q102==1 | hk$Q103==1 | hk$Q104==1 | hk$Q105==1 | hk$Q94==2 | hk$Q95==2 | hk$Q96==2 | hk$Q97==2 | hk$Q98==2 | hk$Q99==2 | hk$Q100==2 | hk$Q101==2 | hk$Q102==2 | hk$Q103==2 | hk$Q104==2 | hk$Q105==2)]<-1
hk$organ<-as.factor(hk$organ)

## Define interpersonal trust ##
hk$pertrust<-NA
hk$Q59<--1*(hk$Q59-5)
hk$Q60<--1*(hk$Q60-5)
hk$Q61<--1*(hk$Q61-5)
hk$Q62<--1*(hk$Q62-5)
hk$Q63<--1*(hk$Q63-5)
hk$pertrust<-(hk$Q59+hk$Q60+hk$Q61+hk$Q62+hk$Q63)/5

## Define educ, which is education of respondent ##
hk$educ<-NA
hk$educ<-hk$Q275
hk$educ<-as.numeric(hk$educ)
## Define income, which is income of household ##
hk$income<-NA
hk$income<-hk$Q288
hk$income<-as.numeric(hk$income)
## Define age, which is age of respondent ##
hk$age<-NA
hk$age<-hk$Q262
hk$ln_age<-log(hk$age)
## Define gender of respondent. sex=0 is male, sex=1 is female ##
hk$sex<-NA
hk$Q260<-hk$Q260-1
hk$sex<-hk$Q260
hk$sex<-as.factor(hk$sex)
## Define single, which indicates if a respondent is single. single=0 is not single, single=1 if single ##
hk$single<-NA
hk$single[which(hk$Q273==1 | hk$Q273==2 | hk$Q273==3 | hk$Q273==4 | hk$Q273==5)]<-0
hk$single[which(hk$Q273==6)]<-1
hk$single<-as.factor(hk$single)

hk_data<-hk[,c("postmat","postmat2","postmat3","pertrust","organ","dissat","econdissat","dem","polint","poldis","educ","income","age","sex","single","ln_age","protest","protest2")]

## Regressions for peaceful demonstrations ##
logit <- glm(protest ~ postmat + dissat + econdissat + income + dem + polint + poldis + pertrust + organ + educ + ln_age + sex + single, data = hk_data, family = "binomial")
summary(logit)
ord <- polr(protest2 ~ postmat + dissat + econdissat + income + dem + polint + poldis + pertrust + organ + educ + ln_age + sex + single, data = hk_data, Hess = TRUE)
summary(ord)
logit_2 <- glm(protest ~ postmat * dissat + econdissat + income + dem + pertrust + organ + educ + ln_age + sex, data = hk_data, family = "binomial")
summary(logit_2)
ord_2 <- polr(protest2 ~ postmat * dissat + econdissat + income + dem + pertrust + organ + educ + ln_age + sex, data = hk_data, Hess = TRUE)
summary(ord_2)

## Run the interaction
## Figure 7 ##
Inter.dissat <- effect('postmat*dissat', logit_2,
                       xlevels=list(dissat = c(1,2,3,4,5,6,7,8,9,10),
                                    postmat = c(0,1,2)),
                       se=TRUE, confidence.level=.95, given.values=c(organ1=0,sex1=1), typical=mean)

#Put data in data frame 
Inter.dissat <- as.data.frame(Inter.dissat)

#Check out what the "head" (first 6 rows) of your data looks like
head(Inter.dissat)

#Create a factor of the IQ variable used in the interaction                   
Inter.dissat$postmat <- factor(Inter.dissat$postmat,
                               levels=c(0,1,2),
                               labels=c("Materialist", "Mixed values", "Post-materialist"))


ggplot(data=Inter.dissat, aes(x=dissat, y=fit, group=postmat))+
  geom_line(size=2, aes(linetype=postmat))+
  scale_linetype_manual(values = c("solid","dotted","twodash")) +
  xlim(0,10)+
  ylim(0,1)+
  ylab("Probability of joining protests")+
  xlab("Dissatisfaction with the political system")+
  ggtitle("Interaction plot")+
  theme(text = element_text(size=30),
        plot.title = element_blank(),
        axis.text = element_text(size=25),
        legend.title = element_blank(),
        legend.text = element_text(size=32),
        legend.key.size = unit(2, 'cm'))

## Plot the main effect of post-materialism ##
## Figure 6a ##
logit <- glm(protest ~ postmat + dissat + econdissat + income + dem + polint + poldis + pertrust + organ + educ + ln_age + sex + single, data = hk_data, family = "binomial")
hk_data$postmat<-factor(hk_data$postmat, levels=c(0,1,2), labels=c("Materialist","Mixed values","Post-materialist"))

effect_plot(logit,postmat,interval = TRUE, x.label = "Post-materialism index", y.label = "Probability",
            main.title = "Predicted probability of joining protests") +
  ylim(0.3,1) +
  theme(text = element_text(size=21),
        plot.title = element_text(size=21),
        axis.text = element_text(size=18),
        axis.title.x = element_blank())

# Plot the main effect of political dissatisfaction #
## Figure 6b ##
effect_plot(logit,dissat,interval = TRUE, x.label = "Dissatisfaction with the political system", y.label = "Probability",
            main.title = "Predicted probability of joining protests") +
  xlim(1,10) +
  ylim(0.4,0.8) +
  theme(text = element_text(size=21),
        plot.title = element_text(size=21),
        axis.text = element_text(size=21))

## convert explanatory variables to categorical variables ##
hk_data$postmat<-factor(hk_data$postmat, levels=c("Materialist","Mixed values","Post-materialist"), labels=c("0","1","2"))
hk_data$polint<-as.factor(hk_data$polint)
hk_data$poldis<-as.factor(hk_data$poldis)
hk_data$educ<-as.factor(hk_data$educ)

logit_cat <- glm(protest ~ postmat + dissat + econdissat + income + dem + polint + poldis + pertrust + organ + educ + ln_age + sex + single, data = hk_data, family = "binomial")
summary(logit_cat)
ord_cat <- polr(protest2 ~ postmat + dissat + econdissat + income + dem + polint + poldis + pertrust + organ + educ + ln_age + sex + single, data = hk_data, Hess = TRUE)
summary(ord_cat)
logit_cat2 <- glm(protest ~ postmat * dissat + econdissat + income + dem + pertrust + organ + educ + ln_age + sex, data = hk_data, family = "binomial")
summary(logit_cat2)
ord_cat2 <- polr(protest2 ~ postmat * dissat + econdissat + income + dem + pertrust + organ + educ + ln_age + sex, data = hk_data, Hess = TRUE)
summary(ord_cat2)


## Regressions with alternative measures of post-materialism (results not shown) ##
logit <- glm(protest ~ postmat + dissat + econdissat + income + dem + polint + poldis + pertrust + organ + educ + ln_age + sex + single, data = hk_data, family = "binomial")
summary(logit)
logit_p2 <- glm(protest ~ postmat2 + dissat + econdissat + income + dem + polint + poldis + pertrust + organ + educ + ln_age + sex + single, data = hk_data, family = "binomial")
summary(logit_p2)
logit_p3 <- glm(protest ~ postmat3 + dissat + econdissat + income + dem + polint + poldis + pertrust + organ + educ + ln_age + sex + single, data = hk_data, family = "binomial")
summary(logit_p3)
logit_all <- glm(protest ~ postmat + postmat2 + postmat3 + dissat + econdissat + income + dem + polint + poldis + pertrust + organ + educ + ln_age + sex + single, data = hk_data, family = "binomial")
summary(logit_all)

ord <- polr(protest2 ~ postmat + dissat + econdissat + income + dem + polint + poldis + pertrust + organ + educ + ln_age + sex + single, data = hk_data, Hess = TRUE)
summary(ord)
ord_p2 <- polr(protest2 ~ postmat2 + dissat + econdissat + income + dem + polint + poldis + pertrust + organ + educ + ln_age + sex + single, data = hk_data, Hess = TRUE)
summary(ord_p2)
ord_p3 <- polr(protest2 ~ postmat3 + dissat + econdissat + income + dem + polint + poldis + pertrust + organ + educ + ln_age + sex + single, data = hk_data, Hess = TRUE)
summary(ord_p3)
ord_all <- polr(protest2 ~ postmat + postmat2 + postmat3 + dissat + econdissat + income + dem + polint + poldis + pertrust + organ + educ + ln_age + sex + single, data = hk_data, Hess = TRUE)
summary(ord_all)
